package com.mvetc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mvetc.entity.Comment;
import com.mvetc.entity.PageInfo;
import com.mvetc.service.CommentService;
import com.mvetc.service.impl.CommentServiceImpl;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	private CommentService commentService = new CommentServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if (op == null || "getCommentList".equals(op)) {
			doGetCommentLisr(request,response);
		}
	}

	private void doGetCommentLisr(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String nameKey = request.getParameter("nameKey");
		String nameValue = request.getParameter("nameValue");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, String> map = new HashMap<>();
		map.put(nameKey, nameValue);
		if (!"userNameKey".equals(nameKey)) {
			map.put("userNameKey", "");
		}else {
			map.put("movieNameKey", "");
		}
		map.put("movieId", "");
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Comment> pageInfo = commentService.getBy(map);
		
		// 利用第三方类库,将数据转换成JSON字符串
		Gson gson = new Gson();
		String jsonObject = gson.toJson(pageInfo);
		// 2.响应
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
	}

}
